﻿@page "/inventory"
@using Microsoft.EntityFrameworkCore
@using Wms.Core.Entities
@inject Wms.Data.WarehouseDbContext Db
@inject IJSRuntime JSRuntime

<div class="container-fluid py-4">
    <h4 class="text-primary mb-4">
        <span class="oi oi-layers me-2"></span> 库存管理
    </h4>

    <!-- 搜索 / 筛选 --> 
    <div class="card shadow-sm mb-4">
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-6">
                    <input type="text" class="form-control form-control-lg" placeholder="搜索物料名称或编码" @bind="searchTerm" />
                </div>
                <div class="col-md-4">
                    <select class="form-select form-select-lg" @bind="selectedWarehouseId">
                        <option value="">全部仓库</option>
                        @foreach (var w in warehouses)
                        {
                            <option value="@w.Id">@w.Name</option>
                        }
                    </select>
                </div>
                <div class="col-md-2">
                    <button class="btn btn-primary btn-lg w-100" @onclick="LoadInventories">查询</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 库存列表 -->
    <div class="card shadow-sm">
        <div class="card-body p-0">
            @if (!inventories.Any())
            {
                <div class="text-center py-5">
                    <span class="oi oi-layers" style="font-size: 48px; opacity: 0.3;"></span>
                    <p class="text-muted mt-3">暂无库存数据</p>
                </div>
            }
            else
            {
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>物料编码</th>
                                <th>物料名称</th>
                                <th>仓库</th>
                                <th>库位</th>
                                <th>批次号</th>
                                <th>库存数量</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var i in inventories)
                            {
                                <tr class="@(i.Quantity < alertThreshold ? "table-warning" : "")">
                                    <td>@i.Item.Code</td>
                                    <td>@i.Item.Name</td>
                                    <td>@i.Location.Warehouse.Name</td>
                                    <td>@i.Location.Code</td>
                                    <td>@i.BatchNo</td>
                                    <td>@i.Quantity</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }
        </div>
    </div>
</div>

@code {
    private List<Wms.Core.Entities.Inventory> inventories = new();
    private List<Warehouse> warehouses = new();
    private string? searchTerm;
    private int? selectedWarehouseId;
    private int alertThreshold = 10; // 库存低于该值高亮

    protected override async Task OnInitializedAsync()
    {
        warehouses = await Db.Warehouses.ToListAsync();
        await LoadInventories();
    }

    private async Task LoadInventories()
    {
        var query = Db.Inventories
            .Include(i => i.Item)
            .Include(i => i.Location)
                .ThenInclude(l => l.Warehouse)
            .AsQueryable();

        if (!string.IsNullOrWhiteSpace(searchTerm))
        {
            query = query.Where(i => i.Item.Name.Contains(searchTerm) || i.Item.Code.Contains(searchTerm));
        }

        if (selectedWarehouseId.HasValue)
        {
            query = query.Where(i => i.Location.WarehouseId == selectedWarehouseId.Value);
        }

        inventories = await query
            .OrderBy(i => i.Item.Name)
            .ThenBy(i => i.Location.Code)
            .ToListAsync();
    }
}
